package com.bazaaroid.mobile.db.model;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

//import com.google.appengine.api.datastore.Key;

@SuppressWarnings("serial")
@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class AbstractModel implements Serializable {

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private int key = 0;
	@Persistent private Date created;
	@Persistent private Date modified;

	public int getKey() {
		return key;
	}

	public void setKey(int key) {
		this.key = key;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public Date getModified() {
		return modified;
	}

	public void setModified(Date modified) {
		this.modified = modified;
	}

	@Override
	public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || !(object instanceof AbstractModel)) return false;

        AbstractModel other = (AbstractModel) object;

        // if the key is missing, return false
        if (key == 0) {
        	return false;
        }

        // equivalence by key
        return key == other.getKey();
    }

	@Override
    public int hashCode() {
        if (key != 0) {
            return key;
        } else {
            return super.hashCode();
        }
    }
}
